import Dao.DaoStuHandle;
import Model.Student;
import Model.User;
import Tools.Yanzhengma;
import UserView.UserUi;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

public class StudentRun {
    public static void main(String[] args) throws Exception {
            System.out.println("--------------------------------------");
            System.out.println("**欢迎使用学生信息管理系统***");
            System.out.println("--------------------------------------");
            for (int i = 0; i < 3; i++) {
                System.out.println("请输入管理员的账户和密码");
                Scanner scanner = new Scanner(System.in);
                System.out.println("账户：|");
                String zh = scanner.next();
                System.out.println("密码：|");
                String pw = scanner.next();
                String code = Yanzhengma.code();
                System.out.println("验证码是：(" + code + ")");
                String usercode = scanner.next();
                if (!usercode.equals(code)) {
                    System.out.println("验证码错误");
                } else {
                    DaoStuHandle.info();
                    User user = new User();
                    user.setSname(zh);
                    user.setPassword(pw);
                    InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis_config.xml");

                    //获取一个工厂对象
                    SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
                    //通过工厂获取一个对象
                    SqlSession sqlSession = build.openSession();
                    List<User> users = sqlSession.selectList("StudentMapper.findAll1",user);
                    System.out.println(users);
                   if (!users.isEmpty()){
                      System.out.println("登录成功！");
                      System.out.println("----请选择你的业务操作----");
                      //生成操作界面   用户表现层
                      UserUi userUi = new UserUi();
                      userUi.UI();
                  }else{
                      System.out.println("登录失败，账号或者密码输入错误");
                  }
                }
                if (i==2){
                    System.out.println("账户被锁定请联系相关人员，电话是：19234434652");
                }
            }
    }
}
